<?php
/**
 * 规则: 10分钟内获取的验证码尚未使用,都会提示"您已获取验证码,10分钟内请勿重复获取"
 * 短信发送类:平台创蓝
 * 2019.7.23
 * QQ:748507607
 * 备注: 测试号码不加白名单限制为:给一个号码发短信,1分钟收到2条,一小时5条,一天限制10条
 */
namespace app\share\model;

use think\Model;

class SendSms extends Model
{

    protected  $table = 'yan_code';
    protected $STATUS_TRUE = 1;//未使用
    protected $STATUS_FALSE = 2;//已使用
    /**
     * 查询10分钟内是否有可用的验证码
     * @param string $mobile  手机号码
     */
    protected function ableUse($mobile)
    {
        $where['code_mobile'] = $mobile;
//        $where['code_status'] = $this->STATUS_TRUE;
        $order = ['code_id'=>'desc'];
        $data = $this->field("code_time, code_status")->where($where)->order($order)->find();
        if( abs( $data['code_time'] - time() ) <= 600  && $data['code_status'] === $this->STATUS_TRUE)
        {
            return $this->STATUS_TRUE;
        }
    }
    /**
     * 验证码入库,10分钟有效,10分钟不重复发送
     * @param string $code 			短信code
     */
    protected function insertCode( $mobile, $code )
    {
        $res = $this->insert(['code_mobile'=>$mobile,'code_num'=>$code,'code_time'=>time()]);
        return $res;
    }
    /**
     * 发送短信
     * @param string $mobile 		手机号码
     * @param string $code 			短信code
     * @param string $needstatus 	是否需要状态报告
     */
    public function sendSms( $mobile, $code, $needstatus = 'true') {
        //创蓝接口参数
        $msg = '您的验证码是' . $code . ',10分钟内有效';
        $postArr = array (
            'account'  =>  config('send_sms')['api_account'],
            'password' =>  config('send_sms')['api_password'],
            'msg' => urlencode($msg),
            'phone' => $mobile,
            'report' => $needstatus
        );
        //10分钟内请勿重复获取未使用的验证码
        if($this->ableUse($mobile)===$this->STATUS_TRUE)
        {
            return ['code'=>200, 'msg'=>"您已获取验证码,10分钟内请勿重复获取"];
        }

        $result = $this->curlPost( config('send_sms')['api_send_url'],$postArr );
        if(!is_null(json_decode($result))){
            $output=json_decode($result,true);
            if(isset($output['code'])  && $output['code']=='0'){
                $this->insertCode( $mobile, $code );
                return ['code'=>200,'msg'=>"短信发送成功"];
            }else{
                return ['code'=>-1004,'msg'=>$output['errorMsg']];
            }
        }else{
            return ['code'=>-1004,'msg'=>$result];
        }
    }
    /**
     * 通过CURL发送HTTP请求
     * @param string $url  //请求URL
     * @param array $postFields //请求参数
     * @return mixed
     *
     */
    private function curlPost( $url,$postFields ){
        $postFields = json_encode( $postFields );

        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8'
            )
        );
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt( $ch, CURLOPT_TIMEOUT,60);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec ( $ch );
        if (false == $ret) {
            $result = curl_error(  $ch);
        } else {
            $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 ". $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
        curl_close ( $ch );
        return $result;
    }



}